package com.mail.server.controller;


import com.mail.model.db.PmsProductAttributeCategory;
import com.mail.model.vo.PageResult;
import com.mail.model.vo.ProductAttributeVo;
import com.mail.server.common.R;
import com.mail.server.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author WJZ
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {


    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;

    /**
     * 添加商品属性分类
     * @param map
     * @return
     */
    @PostMapping("/create")
    public R addProductAttributeCategory(String name){
        productAttributeCategoryService.addProductAttributeCategory(name);
        return R.success(null);
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable Integer id){
        productAttributeCategoryService.deleteById(Long.valueOf(id));
        return R.success(null);
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R queryProductAttributeList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult = productAttributeCategoryService.queryProductAttributeList(pageNum,pageSize);
        return R.success(pageResult);
    }

    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/list/withAttr")
    public R queryListWithAttr(){
        List<ProductAttributeVo> productAttributeVoList = productAttributeCategoryService.queryListWithAttr();
        return R.success(productAttributeVoList);
    }

    /**
     * 修改商品属性分类
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateAttributeCategory(@PathVariable Integer id,String name){
        productAttributeCategoryService.updateAttributeCategory(Long.valueOf(id),name);
        return R.success(null);
    }


    @GetMapping("/{id}")
    public R queryAttributeCategoryById(@PathVariable Integer id){
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.queryAttributeCategoryById(Long.valueOf(id));
        return R.success(productAttributeCategory);
    }
}
